<template>
	<view class="success">
		<nav-bar></nav-bar>
		<view class="t_container">
			<view class="title">
				肺活量采集完成
			</view>
		</view>
		<view class="m_container">
			<view class="info">
				本次采集肺活量: {{ measureInfo.measureData }}ml
			</view>
			<view class="estimate">
				预估肺活量: {{ measureInfo.estimateHaleVol }}ml
			</view>
			<view class="estimate_tip" style="width: 100%;">
				预估最大呼气量根据您的性别、年龄、身高、体重等基础信息计算所得，仅供为参考作用。个人的实际数值以实际测得为准。
			</view>
			<view class="estimate_tip" style="width: 310rpx;">
				<text style="font-size: 24rpx;">实际预测:</text>
				<text style="font-size: 32rpx;padding-top: 16rpx;">{{ measureInfo.percent }}%</text>
			</view>
			<view class="tips" v-if="abnormal">
				检测到您的三次有效采集数据差异性过大，可能影响后续代谢测量精准度，建议重新开始采集，是否继续。
			</view>
			<view class="check" @click="$u.throttle(goRecord, 2000)" v-if="type == 'default'">
				<view class="content">
					<text> 查看详情</text>
					<text class="iconfont icon-arrow_rigth" style="padding-left: 10rpx;"></text>
				</view>
			</view>
		</view>
		<!-- <view class="handle_c" v-if="type == 'default'">
			<view class="delete_btn" @click="$u.throttle(again, 1500)">
				放弃
			</view>
			<view class="save_btn" @click="$u.throttle(next, 1500)">
				继续
			</view>
		</view> -->
        <view class="large_button" v-if="type == 'default'"  @click="$u.throttle(next, 1500)">
			完成
		</view>
		<view class="large_button" v-else @click="back" >
			完成
		</view>
	</view>
</template>
<script>
	import {
		findExhaleData,
		findRegisterSucceed,
		getDetailsByUser,
		abandonBodyMeasure
	} from '@/api/index.js'
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				paddingHeight: uni.getStorageSync('navbarHeight'),
				measureInfo: undefined,
				type: 'default	',
				abnormal: false // 是否异常
			}
		},
		onLoad(OPTIONS) {
			console.log(OPTIONS)
			this.measureInfo = JSON.parse(OPTIONS.info)
			this.type = this.measureInfo.type
			this.measureInfo.percent = parseInt(this.measureInfo.percent * 100)
			if (this.measureInfo.abnormal) this.abnormal = true
			if (this.measureInfo.popFlag == 1) {
				uni.showToast({
					title: '你的肺活量远远小于正常值',
					icon: 'none',
					duration: 3000
				})
			}
		},
		computed: {
			...mapState({
				ble: state => state.ble, // 实时蓝牙全局状态
				userInfo: state => state.userInfo
			}),
		},
		methods: {
			async next() {
				let params = {
					bodyId: this.userInfo.id
				}
				const res = await findRegisterSucceed(params)
                await this.getInfoByUser()
				if (res.data == 1) {
					uni.redirectTo({
						url: `/pagesB/login/device/measure/tips`
					})
				}
			},
            // 更新用户的肺活量信息
            async getInfoByUser () {
                let params = {
                    userId: this.userInfo.userId,
                    urlencoded: true
                }
                try {
                    const res = await getDetailsByUser(params)
                    if (res.code == 200 && res.data) {
                        this.$store.commit('setUserInfo', res.data)
                    }
                } catch (e) {
                    //TODO handle the exception
                }
            },
			// 重新采集清楚之前数据
			async again() {
				let params = {
					bodyId: this.userInfo.id,
					flag: 1
				}
				try {
					const res = await abandonBodyMeasure(params)
					if (res.code == 200) {
						uni.redirectTo({
							url: '/pagesB/login/device/measure/exhale'
						})
					}
				} catch (e) {
					//TODO handle the exception
				}
			},
			goRecord() {
				uni.navigateTo({
					url: `/pagesB/login/device/record/index?type=all&flag=1`
				})
			},
			async back() {
                if(this.userInfo.off == 1){
                    let params = {
				    	bodyId: this.userInfo.id
				    }
                    await findRegisterSucceed(params)
                    await this.getInfoByUser()

                }
                uni.navigateBack()
			}
		}
	}
</script>
<style lang="scss" scoped>
	.t_container {
		width: 100%;
		height: 556rpx;
		background-image: url('http://oss.antaimedical.com/images/static/2024/08/66ac9a14e4b000d2c606b51bKtP0NbqctY5U39a744c1b547badc117d392dbc405dfb.png');
		background-size: 100% 100%;
		background-repeat: no-repeat;
		padding: 208rpx 80rpx 0;

		.title {
			font-weight: 500;
			font-size: 36rpx;
			color: #FFFFFF;
			font-family: PingFangSC-Medium, PingFang SC;
			text-align: left;
		}
	}

	.m_container {
		width: 100%;
		background-color: #fff;
		border-radius: 48rpx 48rpx 0rpx 0rpx;
		margin-top: -264rpx;
		padding: 48rpx;

		.info {
			padding: 32rpx;
			font-weight: 500;
			font-size: 32rpx;
			color: #545DDE;
			text-align: left;
			font-family: PingFangSC-Medium, PingFang SC;
			border-bottom: 1rpx solid rgba(#000000, .1);
		}

		.estimate {
			padding: 30rpx 32rpx 16rpx;
			font-size: 32rpx;
			color: #333333;
			text-align: left;
		}

		.estimate_tip {
			padding: 32rpx;
			margin-bottom: 32rpx;
			background-color: rgba(#545DDE, .05);
			border-radius: 16rpx;
			line-height: 28rpx;
			font-size: 20rpx;
			color: #666666;
			display: flex;
			flex-direction: column;
			align-items: flex-start;
			text-align: left;
		}

		.tips {
			padding: 32rpx;
			font-weight: 500;
			font-size: 32rpx;
			color: #545DDE;
		}

		.check {
			padding-top: 30rpx;
			width: 100%;
			border-top: 1rpx solid rgba(#000000, .1);

			.content {
				width: 100%;
				height: 88rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				background: rgba(#545DDE, .05);
				border-radius: 16rpx;
				color: #545DDE;
				font-size: 28rpx;
			}

		}
	}

	.large_button {
		position: fixed;
		bottom: 76rpx;
		left: 50%;
		transform: translateX(-50%);
	}
</style>